package com.ruoyi.web.controller.dict;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dict.domain.TDictUrgencyLevel;
import com.ruoyi.dict.service.ITDictUrgencyLevelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 紧急程度Controller
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/system/level")
public class TDictUrgencyLevelController extends BaseController
{
    @Autowired
    private ITDictUrgencyLevelService tDictUrgencyLevelService;

    /**
     * 查询紧急程度列表
     */
    @PreAuthorize("@ss.hasPermi('system:level:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDictUrgencyLevel tDictUrgencyLevel)
    {
        startPage();
        List<TDictUrgencyLevel> list = tDictUrgencyLevelService.selectTDictUrgencyLevelList(tDictUrgencyLevel);
        return getDataTable(list);
    }

    /**
     * 导出紧急程度列表
     */
    @PreAuthorize("@ss.hasPermi('system:level:export')")
    @Log(title = "紧急程度", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TDictUrgencyLevel tDictUrgencyLevel)
    {
        List<TDictUrgencyLevel> list = tDictUrgencyLevelService.selectTDictUrgencyLevelList(tDictUrgencyLevel);
        ExcelUtil<TDictUrgencyLevel> util = new ExcelUtil<TDictUrgencyLevel>(TDictUrgencyLevel.class);
        util.exportExcel(response, list, "紧急程度数据");
    }

    /**
     * 获取紧急程度详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:level:query')")
    @GetMapping(value = "/{code}")
    public AjaxResult getInfo(@PathVariable("code") String code)
    {
        return success(tDictUrgencyLevelService.selectTDictUrgencyLevelByCode(code));
    }

    /**
     * 新增紧急程度
     */
    @PreAuthorize("@ss.hasPermi('system:level:add')")
    @Log(title = "紧急程度", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDictUrgencyLevel tDictUrgencyLevel)
    {
        // 检查编码是否已存在
        if (tDictUrgencyLevelService.selectTDictUrgencyLevelByCode(tDictUrgencyLevel.getCode()) != null)
        {
            return error("编码已存在，请使用其他编码");
        }
        // 设置创建者
        tDictUrgencyLevel.setCreatedBy(getUserId());
        return toAjax(tDictUrgencyLevelService.insertTDictUrgencyLevel(tDictUrgencyLevel));
    }

    /**
     * 修改紧急程度
     */
    @PreAuthorize("@ss.hasPermi('system:level:edit')")
    @Log(title = "紧急程度", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDictUrgencyLevel tDictUrgencyLevel)
    {
        // 检查记录是否存在
        TDictUrgencyLevel existingLevel = tDictUrgencyLevelService.selectTDictUrgencyLevelByCode(tDictUrgencyLevel.getCode());
        if (existingLevel == null)
        {
            return error("要修改的记录不存在，编码：" + tDictUrgencyLevel.getCode());
        }
        
        // 设置修改者
        tDictUrgencyLevel.setUpdatedBy(getUserId());
        
        int result = tDictUrgencyLevelService.updateTDictUrgencyLevel(tDictUrgencyLevel);
        if (result > 0)
        {
            return success("修改成功");
        }
        else
        {
            return error("修改失败，没有记录被更新");
        }
    }

    /**
     * 删除紧急程度
     */
    @PreAuthorize("@ss.hasPermi('system:level:remove')")
    @Log(title = "紧急程度", businessType = BusinessType.DELETE)
	@DeleteMapping("/{codes}")
    public AjaxResult remove(@PathVariable String[] codes)
    {
        return toAjax(tDictUrgencyLevelService.deleteTDictUrgencyLevelByCodes(codes));
    }
}